//
//  CWBaseCollectionViewCell.swift
//  ClockWidget
//
//  Created by fff on 2024/11/12.
//


open class TSBaseCollectionCell: UICollectionViewCell,TSComponentView  {
    
    public var colComponent:TSCollectionViewComponent?
    public var colAttributes:[String : Any]?
    
    public lazy var bgContentView:UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        self.addSubview(bgContentView)
        bgContentView.snp.makeConstraints { make in
            make.top.leading.trailing.bottom.equalTo(0)
        }
        creatUI()
        dealThings()
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open func creatUI(){
        
    }
    
    open func dealThings(){
        
    }
    
    open func renderView(with object: Any?, component: TSCollectionViewComponent, attributes: [String : Any]?) {
        self.colComponent = component
        self.colAttributes = attributes
    }
    
    deinit {
//        debugPrint("<---deinit \(String(describing: self))")
    }
    
    public var cellIndexPath:IndexPath?{
        if let attributes = colAttributes , let IndexPath = attributes[kIndexPath] as? IndexPath{
            return IndexPath
        }
        return nil
    }
    
    public var itemActionHandler: ((Any?, IndexPath) -> Void)?{
        if let colComponent = colComponent,let itemActionHandler = colComponent.itemActionHandler {
            return itemActionHandler
        }
        return nil
    }
    
    public func actionHandler(any:Any?){
        if let sectionActionHandler = itemActionHandler ,let indexPath = cellIndexPath{
            sectionActionHandler(any,indexPath)
        }
    }
    
    open var cellDisplayIndex:IndexPath?
    open var isCellDisplay:Bool = false
    open func willDisplay(indexPath:IndexPath) {
        cellDisplayIndex = indexPath
        isCellDisplay = true
    }
    
    open func didEndDisplaying(indexPath:IndexPath) {
        cellDisplayIndex = indexPath
        isCellDisplay = false
    }
}


open class TSBaseCollectionnReusableView : UICollectionReusableView ,TSComponentView  {
    
    public var colComponent:TSCollectionViewComponent?
    public var colAttributes:[String : Any]?
    
    public lazy var bgContentView:UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .clear
        self.addSubview(bgContentView)
        bgContentView.snp.makeConstraints { make in
            make.top.leading.trailing.bottom.equalTo(0)
        }
        creatUI()
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open func creatUI(){
        
    }

    open func renderView(with object: Any?, component: TSCollectionViewComponent, attributes: [String : Any]?) {
        self.colComponent = component
        self.colAttributes = attributes
    }
    
    
    deinit {
//        debugPrint("<---deinit \(String(describing: self))")
    }
    
}

extension TSBaseCollectionnReusableView{
    public var cellIndexPath:IndexPath?{
        if let attributes = colAttributes , let IndexPath = attributes[kIndexPath] as? IndexPath{
            return IndexPath
        }
        return nil
    }
    
    public var sectionActionHandler: ((Any?, IndexPath) -> Void)?{
        if let colComponent = colComponent,let sectionActionHandler = colComponent.sectionActionHandler {
            return sectionActionHandler
        }
        return nil
    }
    
    public func actionHandler(any:Any?){
        if let sectionActionHandler = sectionActionHandler ,let indexPath = cellIndexPath{
            sectionActionHandler(any,indexPath)
        }
    }
}
